package edu.towson.cis.cosc603.project5.vendingmachine;

import static org.junit.Assert.*;

import org.junit.rules.ExpectedException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;


/**
 * The unit test Class for VendingMachineItem.
 */
public class VendingMachineItemTest {

    /** Declaring necessary test objects for {@link VendingMachineItem} */
    VendingMachineItem item1;

    /** Declare expected exception */
    @Rule
    public ExpectedException exception = ExpectedException.none();

    /**
     * Initializes the necessary test objects for the test cases to use.
     *
     * @throws Exception the exception
     */
    @Before
    public void setUp() throws Exception {

        item1 = new VendingMachineItem("Coca-Cola", 0.75);

    }

    /**
     * Test for the VendingMachineItem() constructor of the {@link VendingMachineItem} class.
     */
    @Test
    public void testConstructor() {

        assertEquals("Coca-Cola", item1.getName());
        assertEquals(0.75, item1.getPrice(), 0.001);

    }

    /**
     * Test for the VendingMachineItem() constructor of the {@link VendingMachineItem} class.
     *
     * Should throw VendingMachineException when the price is negative
     */
    @Test
    public void testConstructorThrowLessThanZeroException() {

        exception.expect(VendingMachineException.class);
        exception.expectMessage(VendingMachineItem.PRICE_LESS_THAN_ZERO_MESSAGE);

        VendingMachineItem item2 = new VendingMachineItem("Snickers", -0.50);

    }

    /**
     * Test for the getName() method of the {@link VendingMachineItem} class.
     */
    @Test
    public void testGetName() {
        assertEquals("Coca-Cola", item1.getName());
    }

    /**
     * Test for the getPrice() method of the {@link VendingMachineItem} class.
     */
    @Test
    public void testGetPrice() {
        assertEquals(0.75, item1.getPrice(), 0.001);
    }
    
    /**
     * Test for the getNumberOfDecimalPlaces() method of the {@link VendingMachine} class.
     */
    @Test
    public void testGetNumberOfDecimalPlaces() {
        assertEquals(2, item1.getNumberOfDecimalPlaces(0.25));
    }
    
    /**
     * Test for the getNumberOfDecimalPlaces() method of the {@link VendingMachine} class.
     */
    @Test
    public void testIsAmountValid() {
        assertEquals(false, item1.isAmountInvalid(0.25));
        assertEquals(true, item1.isAmountInvalid(0.255));
    }


    /**
     * Cleans up test objects after a test case is executed.
     */
    @After
    public void tearDown() {
        item1 = null;
    }
}
